﻿@page "/Lines/Properties"
@using LiveChartsCore.SkiaSharpView.Blazor
@using LiveChartsCore;
@using LiveChartsCore.SkiaSharpView;
@using LiveChartsCore.SkiaSharpView.Painting;
@using SkiaSharp;

<div>
    <button type="button" class="btn btn-primary" @onclick="ChangeValuesInstance">Values</button>
    <button type="button" class="btn btn-primary" @onclick="ChangeSeriesInstance">Series</button>
    <button type="button" class="btn btn-primary" @onclick="NewFill">Fill</button>
    <button type="button" class="btn btn-primary" @onclick="NewStroke">Stroke</button>
    <button type="button" class="btn btn-primary" @onclick="NewGeometryFill">Geometry fill</button>
    <button type="button" class="btn btn-primary" @onclick="NewGeometryStroke">Geometry stroke</button>
    <button type="button" class="btn btn-primary" @onclick="IncreaseLineSmoothness">+ smooth</button>
    <button type="button" class="btn btn-primary" @onclick="DecreaseLineSmoothness">- smooth</button>
    <button type="button" class="btn btn-primary" @onclick="IncreaseGeometrySize">+ geo size</button>
    <button type="button" class="btn btn-primary" @onclick="DecreaseGeometrySize">- geo size</button>
</div>

<CartesianChart
    Series="@series">
</CartesianChart>

@code {
    private LineSeries<double> _lineSeries;
    private ISeries[] series;
    private Random _random = new Random();

    protected override void OnInitialized()
    {
        _lineSeries = new LineSeries<double>
        {
            Values = new double[] { -2, -1, 3, 5, 3, 4, 6 },
            Stroke = new SolidColorPaint(SKColors.Black),
            Fill = new SolidColorPaint(SKColor.Parse("#30000000")),
            GeometryStroke = new SolidColorPaint(SKColors.Black),
            GeometryFill = new SolidColorPaint(SKColor.Parse("#30000000")),
            LineSmoothness = 0.5,
            GeometrySize = 20
        };
        series = new ISeries[] { _lineSeries };
    }

    private void ChangeValuesInstance()
    {
        var t = 0;
        var values = new double[10];
        for (var i = 0; i < 10; i++)
        {
            t += _random.Next(-5, 10);
            values[i] = t;
        }
        _lineSeries.Values = values;
    }

    private void NewStroke() => _lineSeries.Stroke = new SolidColorPaint(GetRandomColor());
    private void NewFill() => _lineSeries.Fill = new SolidColorPaint(GetRandomColor());
    private void NewGeometryFill() => _lineSeries.GeometryFill = new SolidColorPaint(GetRandomColor());
    private void NewGeometryStroke() => _lineSeries.GeometryStroke = new SolidColorPaint(GetRandomColor());

    private void IncreaseLineSmoothness()
    {
        if (_lineSeries.LineSmoothness >= 1) return;
        _lineSeries.LineSmoothness += 0.1;
    }

    private void DecreaseLineSmoothness()
    {
        if (_lineSeries.LineSmoothness <= 0) return;
        _lineSeries.LineSmoothness -= 0.1;
    }

    private void IncreaseGeometrySize()
    {
        if (_lineSeries.GeometrySize >= 60) return;
        _lineSeries.GeometrySize += 10;
    }

    private void DecreaseGeometrySize()
    {
        if (_lineSeries.GeometrySize <= 0) return;
        _lineSeries.GeometrySize -= 10;
    }

    private void ChangeSeriesInstance()
    {
        _lineSeries = new LineSeries<double>
        {
            Values = new double[] { -2, -1, 3, 5, 3, 4, 6 },
            Stroke = new SolidColorPaint(SKColors.Black),
            Fill = new SolidColorPaint(SKColor.Parse("#30000000")),
            GeometryStroke = new SolidColorPaint(SKColors.Black),
            GeometryFill = new SolidColorPaint(SKColor.Parse("#30000000")),
            LineSmoothness = 0.5,
            GeometrySize = 20
        };
        series = new ISeries[] { _lineSeries };
    }

    private SKColor GetRandomColor()
    {
        var r = _random.Next(0, 255);
        var g = _random.Next(0, 255);
        var b = _random.Next(0, 255);
        return new SKColor((byte)r, (byte)g, (byte)b);
    }
}
